package com.xf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xf.common.core.entity.QueryRequest;
import com.xf.common.core.entity.dmp.DmpOrganization;
import com.xf.mapper.OrganizationMapper;
import com.xf.service.OrganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租户管理
 *
 * @author xufeng
 * @version 1.0
 * @date 2021/10/20
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, DmpOrganization> implements OrganizationService {



    /**
     * 创建工作空间
     *
     * @param organization
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DmpOrganization organization) {
//        //验证开始
//        if (!"admin".equals(FebsUtil.getCurrentUsername())) {
//            throw new FebsException("非admin用户无法创建工作空间");
//        }
//        //1.创建租户 租户1 admin为超级管理员
//        //创建工作空间
//        this.save(organization);
//        //2.创建默认的角色
//        List<Role> roleList = iRoleService.list(new LambdaQueryWrapper<Role>().eq(Role::getIsDefault, YesOrNoEnum.Yes.getValue()));
//        List<RoleMenu> roleMenuList = new ArrayList<>();
//        Map<String, List<RoleMenu>> map = new HashMap<>();
//        roleList.forEach(role -> {
//            List<RoleMenu> roleMenus = iRoleMenuService.getRoleMenusByRoleId(String.valueOf(role.getRoleId()));
//            map.put(role.getRoleId().toString(), roleMenus);
//        });
//        // 3.创建普通管理员的的菜单 t_role_menu
//        OrganizationBroker.runAs(organization.getOrganizationId(), (organizationId -> {
//            roleList.forEach(role -> {
//                List<RoleMenu> roleMenus = map.get(role.getRoleId().toString());
//                role.setTenantId(null);
//                role.setRoleId(null);
//                //新增角色
//                iRoleService.save(role);
//                roleMenus.forEach(roleMenu -> {
//                    roleMenu.setRoleId(role.getRoleId());
//                    roleMenu.setTenantId(null);
//                });
//                iRoleMenuService.saveBatch(roleMenus);
//
//            });
//
//        }));

    }

    @Override
    public IPage<DmpOrganization> getPage(QueryRequest request, DmpOrganization organization) {
        LambdaQueryWrapper<DmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(organization.getOrganizationName())) {
            queryWrapper.eq(DmpOrganization::getOrganizationName, organization.getOrganizationName());
        }
        Page<DmpOrganization> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }
}
